﻿using Dot5Demo.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;

namespace Dot5Demo
{
    public partial class LoadingForm : Form
    {
        public Action<LoadingForm> BackgroundWorkAction
        {
            get;
            set;
        }

        private BackgroundWorker updateDBWorker = new BackgroundWorker();

        /// <summary>
        /// 设置当前执行进度及任务名称，key:任务进度，取值0-100  value：当前任务名称
        /// </summary>
        /// <value>The current MSG.</value>
        public KeyValuePair<int, string> CurrentMsg
        {
            set
            {
                this.updateDBWorker.ReportProgress(value.Key, value.Value);
            }
        }
        public int CurrentProgress
        {
            set
            {
                this.updateDBWorker.ReportProgress(value);
            }
        }

        public LoadingForm()
        {
            InitializeComponent();
            this.ShowIcon = false;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.ControlBox = false;
            this.Text = "资源加载。。。";
            this.StartPosition = FormStartPosition.CenterScreen;
            this.updateDBWorker.WorkerReportsProgress = true;
            this.updateDBWorker.WorkerSupportsCancellation = true;
            this.updateDBWorker.DoWork += new DoWorkEventHandler(this.backgroundWorker1_DoWork);
            this.updateDBWorker.ProgressChanged += new ProgressChangedEventHandler(this.backgroundWorker1_ProgressChanged);
            this.animateImageCtl1.Image = Resources.loading6;
            this.labelCtl1.Text = "开始加载资源";
            this.MinimumSize = this.Size;
            this.MaximumSize = this.Size;
            this.SizeGripStyle = SizeGripStyle.Hide;
            this.Load += LoadingForm_Load;
        }

        private void LoadingForm_Load(object sender, EventArgs e)
        {
            if (IsDesignMode())
                return;
            this.updateDBWorker.RunWorkerAsync();
        }

        public static bool IsDesignMode()
        {
            bool returnFlag = false;
            if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)
            {
                returnFlag = true;
            }
            else if (Process.GetCurrentProcess().ProcessName == "devenv")
            {
                returnFlag = true;
            }
            return returnFlag;
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            SetMessage((e.UserState == null) ? "" : e.UserState.ToString(), e.ProgressPercentage);
        }

        private void SetMessage(string strText, int intValue)
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new MethodInvoker(delegate () { SetMessage(strText, intValue); }));
            }
            else
            {
                BindingProcessMsg(strText, intValue);
            }
        }

        /// <summary>
        /// 设置进度信息，重写此函数可以处理界面信息绑定
        /// </summary>
        /// <param name="strText">进度任务名称</param>
        /// <param name="intValue">进度值</param>
        protected virtual void BindingProcessMsg(string strText, int intValue)
        {
            if (string.IsNullOrEmpty(strText))
            {
                strText = this.labelCtl1.Tag.ToString();
            }
            var txt = string.Format("{0}\t\t{1}%", strText, intValue);
            this.labelCtl1.Text = txt;
            this.labelCtl1.Tag = strText;
            if (intValue > this.progressBar1.Maximum)
                this.progressBar1.Value = this.progressBar1.Maximum;
            else
                this.progressBar1.Value = intValue;
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            if (this.BackgroundWorkAction != null)
            {
                this.BackgroundWorkAction(this);
            }
            Thread.Sleep(100);
            if (base.InvokeRequired)
            {
                base.BeginInvoke(new MethodInvoker(delegate
                {
                    this.Close();
                    this.DialogResult = DialogResult.OK;
                }));
            }
            else
            {
                this.Close();
                this.DialogResult = DialogResult.OK;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
            this.DialogResult = DialogResult.OK;
        }
    }
}